using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using common = Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using business = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business;

using Microsoft.ConnectionEngine.Common.Security;


namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Facade
{
    class ServiceProviderPoolManager
    {
        private ServiceProviderPoolManager() { }

        //ServiceProviderPoolInsert 1027
        public static void ServiceProviderPoolInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            String poolID = xmlMessage.SelectSingleNode("//pnp:PoolID", nsmgr).InnerText;
            String serviceProviderID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderID", nsmgr).InnerText;

            //String status = business.ServiceProviderManager.AddProviderToPool(poolID, serviceProviderID);
            business.ServiceProviderManager.AddProviderToPool(poolID, serviceProviderID);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        // 9003
        public static void ServiceProviderPoolsDelete(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            //admin and the service provider can do this:          
            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal user = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode sptpoolLinksNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderPoolLinks", nsmgr);
            ServiceProviderPoolLinks newpoolsptLinks = (ServiceProviderPoolLinks)common.Utilities.DeserializeObject(sptpoolLinksNode.OuterXml, typeof(ServiceProviderPoolLinks));

            //String status = business.ServiceProviderManager.ServiceProviderPoolLinkDelete(newpoolsptLinks);
            business.ServiceProviderManager.ServiceProviderPoolLinkDelete(newpoolsptLinks);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        //ServiceProviderPoolsInsert 9004
        public static void ServiceProviderPoolsInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            //admin and the service provider can do this:          
            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal user = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode sptpoolLinksNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderPoolLinks", nsmgr);
            ServiceProviderPoolLinks newpoolsptLinks = (ServiceProviderPoolLinks)common.Utilities.DeserializeObject(sptpoolLinksNode.OuterXml, typeof(ServiceProviderPoolLinks));

            //String status = business.ServiceProviderManager.ServiceProviderPoolLinkInsert(newpoolsptLinks);
            business.ServiceProviderManager.ServiceProviderPoolLinkInsert(newpoolsptLinks);
            
            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }


        //ServiceProviderPoolDelete 1026
        public static void RemoveServiceProviderFromPool(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            String poolID = xmlMessage.SelectSingleNode("//pnp:PoolID", nsmgr).InnerText;
            String serviceProviderID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderID", nsmgr).InnerText;

            //String status = business.ServiceProviderManager.RemoveProviderFromPool(poolID, serviceProviderID);
            business.ServiceProviderManager.RemoveProviderFromPool(poolID, serviceProviderID);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }
			
    }
}
